using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using FeedGatheringService.Properties;

namespace FeedGatheringService
{
    [Serializable]
    public class Feed : BaseEntity
    {
        #region Fields

        private string title;
        private string url;
        private string lastPostGuidName;
        private bool lastPostGuidNameChanged = false;
        
        #endregion

        #region Constructors

        public Feed()
        {
        }

        public Feed(int id, DateTime createdDate, string title, string url, string lastPostGuidName) : base(id, createdDate)
        {
            this.title = title;
            this.url = url;
            this.lastPostGuidName = lastPostGuidName;
            this.lastPostGuidNameChanged = false;
        }

        #endregion

        #region Properties

        public string Url
        {
            get { return url; }
            set { url = value; }
        } 

        public string Title
        {
            get { return title; }
            set { title = value; }
        }

        public string LastPostGuidName
        {
            get { return lastPostGuidName; }
            set
            {
                if (lastPostGuidName != value)
                    this.lastPostGuidNameChanged = true;
                lastPostGuidName = value;
            }
        }

        public bool LastPostGuidNameChanged
        {
            get { return lastPostGuidNameChanged; }
            set { lastPostGuidNameChanged = value; }
        }

        #endregion

        #region Methods

        public void Update()
        {
            using (SqlConnection cn = new SqlConnection(Settings.Default.BlogRollConnectionString))
            {
                string sql = "UPDATE Feed SET LastPostGuidName = @lastPostGuidName, Title = @title,  Url = @url WHERE ID = @ID";
                using (SqlCommand cmd = new SqlCommand(sql, cn))
                {
                    cn.Open();
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("@lastPostGuidName", this.lastPostGuidName);
                    cmd.Parameters.AddWithValue("@title", this.title);
                    cmd.Parameters.AddWithValue("@url", this.url);
                    cmd.Parameters.AddWithValue("@ID", this.Id);
                    cmd.ExecuteNonQuery();
                    cn.Close();
                }
                // reset the flag
                this.lastPostGuidNameChanged = false;
            }
        }

        #endregion

        #region Static Methods

        public static List<Feed> GetList()
        {
            List<Feed> feedList = new List<Feed>();
            using (SqlConnection cn = new SqlConnection(Settings.Default.BlogRollConnectionString))
            {
                cn.Open();
                string sql = "SELECT ID, Title, Url, LastPostGuidName, DateCreated FROM Feed ORDER BY Sequence";
                using (SqlCommand cmd = new SqlCommand(sql, cn))
                {
                    cmd.CommandType = CommandType.Text;
                    SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                    while (rdr.Read())
                    {
                        feedList.Add(new Feed(Convert.ToInt32(rdr["Id"]), Convert.ToDateTime(rdr["DateCreated"].ToString()), rdr["Title"].ToString(), rdr["Url"].ToString(), rdr["LastPostGuidName"].ToString()));
                    }
                }
            }
            return feedList;
        }

        #endregion
    }
}
